import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';
import { Observable, of, Subject } from 'rxjs';
import { catchError, filter, map } from 'rxjs/operators';
import { ConfigService } from 'app/config/config.service';
import { EQUIPMENT_API_URL, ALARM_API_URL } from 'app/app.constants';
import TileWMS from 'ol/source/TileWMS.js';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer.js';
export const httpOptions: Object = {
    headers: new HttpHeaders({
        'Content-Type': 'application/json'
        // 'Authorization': 'my-auth-token',
        // Accept: 'text/plain'
    })
    // responseType: 'text'
};
@Injectable({
    providedIn: 'root'
})
export class DataVisualizationService {
    private apiUrl_report_online = this.config.apiUrl.report;
    private geoServiceUrl = this.config.apiUrl.gis;
    public events: Event[] = [];
    constructor(private http: HttpClient, private config: ConfigService) {}
    /* queryNO-----
        101:Equipment analytics - VMS
        102:Equipment analytics - Video Detector
        103:Equipment analytics - CCTV
        104:Equipment analytics - Signal controller
        105:Traffic analytics - Vehicle flow comparison
        106:Traffic analytics - Real-Time Traffic flow in city
        107:Traffic analytics - The most smooth three roads
        108:Traffic analytics - The most congestion three roads
        109:Traffic analytics - Current road congestion ratio
        110:Traffic analytics - Yesterday road congestion ratio
        111:incident analytics - Statics(by Incident Category)
        112:incident analytics - Statics(by Zone)
        113:incident analytics - Statics(by Month)
        114:incident analytics - Statics(by Incident Reason)
        115:OverAll - Alert Statistics
            params ----  startTime=2010-08-23,endTime=2019-08-23,alertType=CONG
    */
    getIncidentAnalyticsReport(queryNO, params = '='): Observable<any> {
        const url = `${this.apiUrl_report_online}/vnqnreportmgt/trafficReport/get?queryNO=${queryNO}&paras=${params}`;
        return this.http.get<any>(url);
    }
    getIncidentAnalyticsWeekReport(queryNO, params = '='): Observable<any> {
        const url = `${this.apiUrl_report_online}/vnqnreportmgt/trafficReport/get?queryNO=${queryNO}&paras=${params}`;
        return this.http.get<any>(url);
    }
    // 根据年份查询
    getIncidentAnalyticsYearReport(queryNO, params = '='): Observable<any> {
        const url = `${this.apiUrl_report_online}/vnqnreportmgt/trafficReport/getYearlyData?queryNO=${queryNO}&paras=${params}`;
        return this.http.get<any>(url);
    }

    getTrafficDataLayer() {
        // tslint:disable-next-line:max-line-length
        const url =
            this.geoServiceUrl +
            `/geoserver/HaLong/wms?service=WMS&version=1.1.0&request=GetMap&layers=HaLong:VMS_export_view&` +
            `bbox=107.049641400031,20.939052400348,107.160077300127,21.0103040852654&width=768&height=495&srs=EPSG:4326&format=application/openlayers`;
        const tile = new TileWMS({
            url,
            /* params: {
                service: 'WMS',
                version: '1.1.0',
                request: 'GetMap',
                layers: 'VietnamTest:VietnamTest',
                bbox: 101.888824462891 + ',' + 8.33772850036621 + ',' + 109.493980407715 + ',' + 23.5692386627197,
                width: 383,
                height: 768,
                srs: 'EPSG:4326',
                format: 'application/openlayers'
            }, */
            crossOrigin: 'Anonymous'
        });

        return new TileLayer({
            visible: false,
            source: tile,
            zIndex: 12
        });
    }
    // type  0:转换为时间戳， 1:转换为显示时间
    changeDate(dates, type: 0 | 1, isTime = false) {
        if (!dates) {
            return null;
        }
        let dated;
        switch (type) {
            case 0: {
                dated = new Date(dates).getTime();
                break;
            }
            case 1: {
                const temp = new Date(dates);
                const year = temp.getFullYear();
                const month = temp.getMonth() + 1 >= 10 ? temp.getMonth() + 1 : '0' + (temp.getMonth() + 1);
                const day = temp.getDate() >= 10 ? temp.getDate() : '0' + temp.getDate();
                const hours = temp.getHours() >= 10 ? temp.getHours() : '0' + temp.getHours();
                const min = temp.getMinutes() >= 10 ? temp.getMinutes() : '0' + temp.getMinutes();
                const seconds = temp.getSeconds() >= 10 ? temp.getSeconds() : '0' + temp.getSeconds();
                // dated = temp.getFullYear() + '-' + (temp.getMonth() + 1) + '-' + temp.getDate() + ' ' + temp.getHours() + ':' + temp.getMinutes();
                if (isTime) {
                    dated = day + '/' + month + '/' + year + ' ' + hours + ':' + min + ':' + seconds;
                } else {
                    dated = day + '/' + month + '/' + year;
                }
                break;
            }
        }
        return dated;
    }

    getWeather(lat, lon): Observable<any> {
        const url = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=59060675fb27fdbe15d371dcf1d583c4`;
        return this.http.jsonp(url, 'callback');
    }
}
